#include <QMenu>
#include <QTabBar>
#include <QPushButton>
#include "name-tab-dialog.h"
#include "section-view-manager.h"
#include "section-view-tab-widget.h"
#include "tab-bar.h"
#include "tab-close-button.h"

SectionViewTabWidget::SectionViewTabWidget (QWidget *parent) :
    QTabWidget(parent), m_nameTabDialog(0)
{
    m_tabBar = new TabBar(this);
    setTabBar(m_tabBar);
    m_tabBar->setSelectionBehaviorOnRemove(QTabBar::SelectLeftTab);
    m_closeButton = new TabCloseButton(this);
    connect (m_closeButton, &TabCloseButton::clicked, this, &SectionViewTabWidget::closeTab);
    setCornerWidget(m_closeButton);

    addTab(new QWidget(this), "+");
    setTabToolTip(0, "Dodaj kartę");
    createTab();
    m_tabBar->setContextMenuPolicy(Qt::CustomContextMenu);

    connect (m_tabBar, &TabBar::addTabRequested, this, &SectionViewTabWidget::createTab);
    connect (m_tabBar, &TabBar::customContextMenuRequested, this, &SectionViewTabWidget::showContextMenu);
    connect (m_tabBar, &TabBar::doubleClicked, this, &SectionViewTabWidget::renameTab);
}

SectionViewTabWidget::~SectionViewTabWidget()
{
}

void SectionViewTabWidget::createTab()
{
    SectionViewManager* sectionViewManager = new SectionViewManager(this);
    connect (sectionViewManager, &SectionViewManager::notifyHint, this, &SectionViewTabWidget::notifyHint);
    connect (sectionViewManager, &SectionViewManager::notifyInformation, this, &SectionViewTabWidget::notifyInformation);
    connect (sectionViewManager, &SectionViewManager::notifyWarning, this, &SectionViewTabWidget::notifyWarning);
    insertTab(count()-1, sectionViewManager, QString("Karta %1").arg(count()));
    setCurrentIndex(count()-2);
    sectionViewManager->initialize();
}

void SectionViewTabWidget::renameTab()
{
    m_nameTabDialog = new NameTabDialog(this);
    m_nameTabDialog->setDialogMode(NameTabDialog::RenameTabMode);
    m_nameTabDialog->setExistingNames(tabNames());
    m_nameTabDialog->setAttribute(Qt::WA_DeleteOnClose);
    connect (m_nameTabDialog, &NameTabDialog::accepted, this, &SectionViewTabWidget::finishRenameTab);
    m_nameTabDialog->open();
}

void SectionViewTabWidget::finishRenameTab()
{
    setTabText(currentIndex(), m_nameTabDialog->name());
}

void SectionViewTabWidget::closeTab()
{
    delete widget(currentIndex());
    if (count() == 1)
        createTab();
}

QList<QString> SectionViewTabWidget::tabNames() const
{
    QList<QString> tabNames;
    for (int i = 0; i < count()-1; i++)
        tabNames.append(tabText(i));
    return tabNames;
}

void SectionViewTabWidget::showContextMenu (const QPoint& point)
{
    int tabIndex = tabBar()->tabAt(point);
    if (tabIndex == count()-1)
        return;

    QMenu* menu = new QMenu(this);
    QAction* renameAction = new QAction("Zmień nazwę karty", menu);
    connect (renameAction, &QAction::triggered, this, &SectionViewTabWidget::renameTab);
    menu->addAction(renameAction);
    menu->setAttribute(Qt::WA_DeleteOnClose);
    menu->popup(tabBar()->mapToGlobal(point));
}

void SectionViewTabWidget::tabInserted (int index)
{
    QTabWidget::tabInserted(index);
}

void SectionViewTabWidget::tabRemoved (int index)
{
    QTabWidget::tabRemoved(index);
}
